<!--一键登录页面 点击一键登录会校验登录身份 如果登录过了，点击一键登录的时候进系统 如果没有 那么就跳转到beforeLogin-->
<script setup lang="ts">
import { ref } from 'vue'
import { authWechat } from '@/api/wechat/auth'

import { Wechat_info } from '@/constant/wechat_info'
import { showDialog } from 'vant'
// @ts-ignore
import { cookieUtil } from '@/utils/cookie'
import { useRouter } from 'vue-router'
import type { AuthorizeParams } from '@/api/types'
// 是否同意协议
const isAgree = ref(true)
const router = useRouter()
// 这里应该判断 如果用户已经登录就直接跳转页面
const token = cookieUtil.get('user_token')
if (token) {
  router.push('/')
}
// 一键登录函数，点击后调用微信的授权接口
const handleLogin = () => {
  if (isAgree.value) {
    const params: AuthorizeParams = {
      state: '授权登录',
      redirectUrl: Wechat_info.redirectUrl,
      appId: Wechat_info.appId,
    }
    // 授权微信服务器，拿到code
    authWechat(params).then((res: any) => {
      if (res.code === 200) {
        window.location.href = res.msg
      } else {
        showDialog({
          title: '警告',
          message: '授权失败',
        }).then(() => {
          // on close
        })
      }
    })
  } else {
    showDialog({
      title: '警告',
      message: '请先同意《滨州新生结核病筛查系统隐私协议》',
    }).then(() => {
      // on close
    })
  }
}
</script>

<template>
  <div>
    <div class="once-body">
      <van-button round block class="once-button" @click="handleLogin">一键登录</van-button>
      <div>
        <div class="once-agree">
          <van-checkbox shape="square" v-model="isAgree">
            同意《滨州新生结核病筛查系统隐私协议》
          </van-checkbox>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.once-body {
  margin: 0 20px;
  text-align: center;
}

.once-button {
  background-color: #01d1b7;
  color: white;
  font-size: 16px;
  letter-spacing: 2px;
  font-weight: 550;
}

.once-agree {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 25px;
  margin-top: 7px;
  font-size: 12px;
}
</style>
